package com.rayboot.footpoint.view;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rayboot.footpoint.R;
import com.rayboot.footpoint.model.Footprint;

public class GridAdapter extends BaseAdapter {
	private ArrayList<Footprint> mData = null;
	private LayoutInflater mInflater;

	public GridAdapter(Context context, ArrayList<Footprint> data) {
		super();
		// TODO Auto-generated constructor stub
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mData = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return drawView(position, convertView);
	}

	private View drawView(int position, View view) {
		ViewHolder holder = null;

		if (view == null) {
			view = mInflater.inflate(R.layout.item_grid, null);

			holder = new ViewHolder();

			holder.ivPhoto = (ImageView) view.findViewById(R.id.ivPhoto);
			holder.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
			holder.tvDate = (TextView) view.findViewById(R.id.tvDate);
			holder.tvDes = (TextView) view.findViewById(R.id.tvDes);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		Footprint fp = (Footprint) getItem(position);

		holder.tvTitle.setText(fp.mPicFileName);
		holder.tvDate.setText(String.valueOf(fp.mCreateTime));
		holder.tvDes.setText(fp.mName);
		holder.ivPhoto.setBackgroundDrawable(new BitmapDrawable(fp.mPicPath));

		return view;
	}

	private class ViewHolder {
		ImageView ivPhoto;
		TextView tvTitle;
		TextView tvDate;
		TextView tvDes;
	}

}
